package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.TccOnlinePuton;
import com.ys.enume.storage.TccOnlinePutonEnum;
import com.ys.service.storage.TccOnlinePutonService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 线上质检上架
 */
@Controller
@RequestMapping("/web/storage/tccOnlinePuton")
public class WebTccOnlinePutonController {
	private static final Logger logger = LoggerFactory.getLogger(WebTccOnlinePutonController.class);

	@Autowired
	private CommonApiService<TccOnlinePuton> commonApiService;

    @Autowired
    private TccOnlinePutonService tccOnlinePutonService;

    private final Long MENU_ID = MenuId.TCC_OnlinePuton;

    /*
     * 设置货位详情页
     */
    @GetMapping("/toSetChw")
    public String toSetChw(Model model, HttpServletRequest request, HttpServletResponse response,Long id) {
        try {
            Map<String, Object> extraParam = tccOnlinePutonService.getExtraParam(id);

            List<Map<String, Object>> itemList = tccOnlinePutonService.getItemListByPid(id);
            if(itemList==null || itemList.size()<=0){
                itemList = new ArrayList<>();
                Map<String, Object> newInfo = new HashMap<>();
                newInfo.put("putonNum",extraParam.get("putonNum"));//上架数量
                //newInfo.put("spBm",extraParam.get("spBm"));//商品编码
                //newInfo.put("spName",extraParam.get("spName"));//商品名称

                itemList.add(newInfo);
            }

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID,itemList);

            //已上级
            if(extraParam.get("putonStatus")!=null && Integer.parseInt(extraParam.get("putonStatus").toString())== TccOnlinePutonEnum.PutonStatus.YES.value){
                //清空子列表按钮
                subGridConfig.setButtonArr(new ArrayList<>());
                return commonApiService.toDetail(model,Long.parseLong(extraParam.get("id").toString()), new TccOnlinePuton(), MENU_ID, extraParam, subGridConfig);
            }
            return commonApiService.toEdit(model,Long.parseLong(extraParam.get("id").toString()), new TccOnlinePuton(), MENU_ID, extraParam, subGridConfig);

        }catch (Exception e) {
            logger.error("页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


}






